function [objs, vex0s, vey0s, vareas, veas, vebs, vephis, vx1s, vx2s, vy1s, vy2s] = ExtrapolateObjs(objs, vex0s, vey0s, vareas, veas, vebs, vephis, vx1s, vx2s, vy1s, vy2s, nparseobjs, bparseobjs, len, nfindobjs)

i = 1;

nstartframe = 0;
nendframe = 0;
maxframes = 10;


while i < len
    
    % if not all objects detected check to see if one or more objects were not found, or if they combined into a single object
    if nparseobjs(i) > 0 && nstartframe == 0
       nstartframe = i;
       nendframe = i;
    elseif nparseobjs(i) > 0           
       nendframe = i;       
    elseif nstartframe > 0 & nendframe > 0 & objs(i) >= objs(nendframe)
                       
        nframes = nendframe - nstartframe + 1;
                        
		%If not all objects were detected across a span of nframes, and it looks like one or more objects 
		%combined into a single object, then enter here and extrapolate positions of missing objects.
        if nframes > 0 && nframes < maxframes
                   
			%nastframe is the last frame we had all objects before the interval we are extrapolating started. 
			%i is the current frame, and also has all objects. We then extrapolate in the interval between nlastframe and i
			nlastframe = nstartframe-1;
            nobjsprev = objs(nlastframe);
            nobjs = objs(i);                                   
            
            xs = vex0s(nlastframe,:);
            ys = vey0s(nlastframe,:);            
                        
	        	        	        
            %nstartframe is the first frame of the extrapolation interval
	        %We now need to find out which objects we are extrapolating to and from. For example, if object 1 in nlastframe
	        %looks like it is assigned to object 1 in the first frame of the extrapolation period, then we won't use object 1
	        %for extrapolation. If on the other hand object 2 isn't assigned to any of the objects in the extapolation period, 
	        %we then use it to extrapolate one of the missing objects. 
	        assignparseobjs = zeros(nfindobjs, 1);
	    
	        for j = 1:objs(nstartframe)
				x = vex0s(nstartframe,j);
				y = vey0s(nstartframe,j);

				
				if x > 0
					maxd = 100000;
                    aindex = -1;
					
					for k = 1:objs(nlastframe)
						xb = vex0s(nlastframe,k);
						yb = vey0s(nlastframe,k);
							
						dist = sqrt((x-xb)^2+(y-yb)^2);		
						
						if dist < maxd
							aindex = k;	%Object k in nlastframe is assigned to object j in frame np
							maxd = dist;
						end
						
                    end		
                    
                    if aindex > -1
                        assignparseobjs(aindex) = j;
                    end
				end 
            end
                        
            %Assign objects from nlastframe (frame before we start parsing) to objects in current frame i            
            %[adists, assignments] = AssignObjDists(nobjsprev, nobjs, xs, ys, vex0s(i,:), vey0s(i,:));
            [adists, assignments] = AssignObjDists( nobjs, nobjsprev, vex0s(i,:), vey0s(i,:), xs, ys);
                                 
            for j = 1:nobjsprev 							               
                jprev = j;                
                
				jcurrent = assignments(jprev); %jcurrent is the object in frame i and jprev is the object in nlastframe (the frame right before we start parsing)              
				
				% if the object in nlastframe hasn't been assigned to an object in the extrapolation interval then
				% we can use it to extrapolate one of the missing objects				
				if assignparseobjs(jprev) == 0                           
                                                           
                    i1 = nlastframe;
                    i2 = i;
                                                                
                    ar1 = vareas(i1,jprev);
                    a1 = veas(i1,jprev);
                    b1 = vebs(i1,jprev);
                    eph1 = vephis(i1,jprev);            
                    ey1 = vey0s(i1,jprev);
                    ex1 = vex0s(i1,jprev);
            
                    x1a = vx1s(i1,jprev);
                    x2a = vx2s(i1,jprev);
                    y1a = vy1s(i1,jprev);
                    y2a = vy2s(i1,jprev);
                                                
                    ar2 = vareas(i2,jcurrent);
            		a2 = veas(i2,jcurrent);
                    b2 = vebs(i2,jcurrent);
                    eph2 = vephis(i2,jcurrent);            
                    ey2 = vey0s(i2,jcurrent);
                    ex2 = vex0s(i2,jcurrent);
            
                    x1b = vx1s(i2,jcurrent);
                    x2b = vx2s(i2,jcurrent);
                    y1b = vy1s(i2,jcurrent);
                    y2b = vy2s(i2,jcurrent);
                        
                    dar = (ar2-ar1)/(nframes+1);
 	                da = (a2-a1)/(nframes+1);
                    db = (b2-b1)/(nframes+1);
                    deph = (eph2-eph1)/(nframes+1);
                        
                    dex = (ex2-ex1)/(nframes+1);
                    dey = (ey2-ey1)/(nframes+1);
                        
                    dx1 = (x1b-x1a)/(nframes+1);
                    dx2 = (x2b-x2a)/(nframes+1);
                    dy1 = (y1b-y1a)/(nframes+1);
                    dy2 = (y2b-y2a)/(nframes+1);
                                       
                     
                    %We don't want to extrapolate coordinates for an object 
                    %that already has been detected or extracted so find 
                    %first object with a position of zero and extrapolate it.                    				                    
                    n = 1;
                    
                    for k = nstartframe:nendframe                         
                      
                      nparseobj = nparseobjs(k);
                      ncount = 0;
                       
                      for l = 1:nfindobjs
						  x = vex0s(k,l);                                                  						
                          
						  if x == 0 
							 jparse = l;			                                                       
                             
                            
                             %Exra check - make sure that we don't
                             %extrapolate into an existing object (for
                             %example if 2 objects become 1 but in the
                             %check above it seems like one object has
                             %disappeared. In this case we'll end up
                             %extracting one of the objects into the single
                             %object. So check if the coordinates are
                             %within any of the exisiting objects. If not,
                             %then ok to proceed. 
                                 
                             tempx1 = round(x1a+dx1*n);
                             tempx2 = round(x2a+dx2*n);
                             tempy1 = round(y1a+dy1*n);
                             tempy2 = round(y2a+dy2*n);                                                    
                             
                             if ncount < nparseobj                                 
                               
                                 vareas(k,jparse) = ar1+dar*n;
                                 veas(k,jparse) =  a1+da*n;
                                 vebs(k,jparse) = b1+db*n;
                                 vephis(k,jparse) = eph1+deph*n;           
                                 vex0s(k,jparse) = round(ex1+dex*n);
                                 vey0s(k,jparse) = round(ey1+dey*n);                                                                  
            
                                 vx1s(k,jparse) = round(x1a+dx1*n);
                                 vx2s(k,jparse) = round(x2a+dx2*n);
                                 vy1s(k,jparse) = round(y1a+dy1*n);
                                 vy2s(k,jparse) = round(y2a+dy2*n);
                       
                                 objs(k) = objs(k) + 1;    
                                 
                                 ncount = ncount + 1;                             
                           end                                                            
                        end
                                                                      
                     end %for l = 1:nfindobjs  
                 
                     
                    n = n + 1; 
					
                   end %for k = i1+1:i2-1  
                                                          
                end%if assignparseobjs(jprev) == 0
            end %%for l = 1:nfindobjs  
                      
        end %if nframes > 0 && nframes < maxframes
        
        nparseobjs(nstartframe:nendframe) = 0;
        nstartframe = 0;
        nendframe = 0;
             
    end
    
    i = i + 1;
end

